<template>
	<view class="withdrawal">
		<view class="title">提现金额</view>
		<view class="inputBox flex-r-s-c">
			<view class="symbol">¥</view>
			<input type="number" v-model="withdrawalPrice" @blur="mes">
		</view>
		<view class="" style="display: flex;justify-content: space-between;padding: 40rpx;">
			<view class="text" style="margin: 0;">当前可提现金额为： {{comission}} 元</view>
			<view class="" style="color: #3DAD81;"  @click="allGet">
				全部提现
			</view>
		</view>
		<view class="" style="display: flex;justify-content: space-between;padding: 40rpx;">
			<view class="text" style="margin: 0;color: #000;font-size: 28rpx;">当前收款码</view>
			<view class="" style="color: #3DAD81;" @click="submit" >
				<u-upload ref="uUpload" :action="action" :auto-upload="false" class="up" max-count="1" @on-choose-complete="toSuccess" @on-remove="onRemove"></u-upload>
			</view>
		</view>
		<!-- <view class="" v-if="withdrawal_interdict > 0">
			<view class="text" style="font-size: 15px;margin: 4px 20px;">温馨提示:</view>
			<view class="text" style="font-size: 15px;margin: 6px 20px;">本平台提供现金提现功能，提现额度为{{withdrawal_interdict}}元，仅当前金额超过发起提现的金额才可以申请提现，剩余金额可在下次满足前述提现额度时申请提现。（提现一般3-5天内到账，您理解并同意如遇高峰，提现到账时间延长）</view>
		</view>
		 -->
		
		<view class="btn1" @tap.stop="withdrawalBtn">立即提现</view>
		<view class="btn2" @tap.stop="toMoney">查看提现记录</view>
		
	</view>
</template>

<script>
	import base from '@/api/base.js'
	export default {
		data() {
			return {
				action: 'http://www.example.com/upload',
				comission: '',
				withdrawalPrice: '',
				withdrawal_interdict:'',
				imgUrl:''
			};
		},
		onLoad(el) {
			// this.balance = el.balance
			// this.withdrawal_interdict = el.withdrawal_interdict
			this.getUserInfo()
			// this.comission = uni.getStorageSync('commission')?uni.getStorageSync('commission'):0
			// this.getRules()
		},
		methods: {
			mes(e){
				console.log(e.detail.value,'eee')
				if(e.detail.value > this.comission)this.$utils.toast('超过最大可提现数额')
			},
			// 获取用户信息
			getUserInfo(){
				let that = this
				that.$api.getUserInfo().then(res => {
					that.comission = res.data.data.commission
				})
			},
			onRemove(index, lists) {
				this.imgUrl = ''
			},
			allGet(){
				this.withdrawalPrice = this.comission
				// setTimeout(()=>{
				// 	this.withdrawalBtn()
				// },2000)
			},
			submit() {
				this.$refs.uUpload.upload();
			},
			toMoney(){
				uni.navigateTo({
					url:'/pages/my/withdrawRecord/withdrawRecord'
				})
			},
			async toSuccess(e){
				let getavatarUrl = await this.uploadFilePromise(e[0].url)
				this.imgUrl = getavatarUrl.showUrl
				// console.log(this.imgUrl,'eeeeefdddddddddddddd')
				
			},
			uploadFilePromise(avatarUrl) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${base.imgUrl}/common/upload`, // 仅为示例，非真实的接口地址
						filePath: avatarUrl,
						name: 'file',
						formData: {
							user: 'test',
						},
						success: (res) => {
							// console.log(JSON.parse(res.data),'eeeeefdddddddddddddd')
							setTimeout(() => {
								resolve({
									url: JSON.parse(res.data).url,
									showUrl: JSON.parse(res.data).fileName,
								})
							}, 1000)	
						},
						fail(fails) {
							console.log(fails,'fail')
						}
					});
				})
			},
			// getRules(){
			// 	let that = this
			// 	that.$api.rules().then(res => {
			// 		console.log(res.data.data,'提现成功')
			// 		// that.$utils.toast('提现成功')
			// 		// setTimeout(() => {
			// 		// 	uni.navigateBack()
			// 		// },2000)
			// 	})
			// },
			withdrawalBtn(){
				let that = this
				if(that.withdrawalPrice == '') return that.$utils.toast('请输入提现金额') 
				if(that.imgUrl == '') return that.$utils.toast('请选择收款码图片') 
				// if(that.price == '') return that.$utils.toast('超过最高提现额度') 
				// if(Number(that.price) <= Number(that.withdrawal_interdict)){
				that.$api.withdrawalAdd({
					withdrawalSource: 'COMMISSION',
					withdrawalPrice:that.withdrawalPrice,
					imgUrl:that.imgUrl
				}).then(res => {
					that.$utils.toast('提现成功')
					setTimeout(() => {
						uni.navigateBack()
					},2000)
				})
				// }else{
				// 	return that.$utils.toast('低于最低提现额度')
				// }
			}
		}
	}
</script>

<style lang="less">
		
	::v-deep .u-upload  u-list-item u-add-wrap{
		width: 100rpx;
		height: 100rpx;
	}
	.withdrawal{
		width: 100vw;
		.title{
			width: 90%;
			margin: 30rpx auto;
			font-size: 28rpx;
			font-weight: 400;
			color: #474747;
		}
		
		.inputBox{
			width: 90%;
			margin: 30rpx auto;
			padding: 30rpx 0rpx;
			border-bottom: 1px solid #EFEFEF;
			.symbol{
				font-size: 80rpx;
				font-weight: 500;
				color: #000;
				margin-right: 20rpx;
			}
			input{
				height: 80rpx;
				font-size: 80rpx;
			}
		}
		
		.text{
			width: 80%;
			margin: 40rpx auto;
			font-size: 28rpx;
			font-weight: 400;
			color: #7F8084;
		}
		
		.btn1,.btn2{
			width: 90%;
			height: 96rpx;
			text-align: center;
			line-height: 96rpx;
			// border-radius: 96rpx;
			border-radius: 10rpx;
			background-color: #1FC064;
			color: #fff;
			font-size: 28rpx;
			font-weight: 500;
			position: fixed;
			left: 50%;
			bottom: 380rpx;
			transform: translateX(-50%);
		}
		.btn2{
			bottom: 280rpx;
			color: #1FC064;
			background-color: transparent;
		}
		
	}
</style>
